package com.zhipu.balance.ui.home.record

import android.os.Bundle
import android.view.MotionEvent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.R
import com.zhipu.balance.databinding.ActivityRecordBinding
import com.zhipu.balance.ui.BaseActivity
import com.zhipu.balance.ui.home.calculate.adapter.ProfitOrderAdapter
import com.zhipu.balance.ui.vm.home.record.RecordViewModel
import com.zhipu.balance.utils.InitializeUtils


/***
 * 使用记录
 */
class RecordActivity : BaseActivity<ActivityRecordBinding>() {

    //VM
    private var mRecordViewModel: RecordViewModel? = null

    override fun initCreate() {
        super.initCreate()
        InitializeUtils.AppComponent()?.doInjectionMain(this)
        mRecordViewModel = ViewModelProvider(this, viewModelFactory)[RecordViewModel::class.java]
    }


    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_record
    }


    override fun initView() {
        super.initView()
        binding.viewModel = mRecordViewModel
    }


    override fun initViewObservable() {
        super.initViewObservable()
        /**
         * Fragment回调
         */
        mRecordViewModel?.mFragments?.observe(this, Observer { t ->
            var mProfitOrderAdapter = ProfitOrderAdapter(supportFragmentManager, t!!)
            binding.NoScrollViewPager.adapter = mProfitOrderAdapter
        })
        /**
         * 位置
         */
        mRecordViewModel?.mPosition?.observe(this, Observer {
            binding.NoScrollViewPager.currentItem = it
        })
        /**
         *  主类
         */
        mRecordViewModel?.mMainClass?.observe(this, Observer {
            /**
             * 更新数据
             */
            mRecordViewModel?.UpdateDataMainClass()
        })

        /**
         * NiceSpinner
         */
        binding.MainCass.setOnSpinnerItemSelectedListener { parent, view, position, id ->
            var mMainClass: String = binding.MainCass.selectedItem as String
            mRecordViewModel?.mMainClass?.postValue(mMainClass)

        }

        /**
         * 子类集合
         */
        mRecordViewModel?.mSubclassList?.observe(this, Observer {
            binding.Subclass.attachDataSource(it)

        })
        /**
         * 子类
         */
        mRecordViewModel?.mSubclass?.observe(this, Observer {
            /**
             * 更新子类数据
             */
            mRecordViewModel?.UpdateDataSubclass()
        })


        /**
         * NiceSpinner
         */
        binding.Subclass.setOnSpinnerItemSelectedListener{ parent, view, position, id ->
            var mSubclass: String = binding.Subclass.selectedItem as String
            mRecordViewModel?.mSubclass?.postValue(mSubclass)
        }

        //细分
        mRecordViewModel?.mImperfectSubdivision?.observe(this, Observer {

            SPUtils.getInstance().put("ImperfectSubdivision", it)
            /**
             * 更新子类数据
             */
            mRecordViewModel?.UpdateDataSubclass()
        })


        /**
         * 当前页数
         */
        mRecordViewModel?.atPresentPage?.observe(this, Observer {
            binding.CurrentPageCount.text = "第${it}页"
        })
        /**
         * 总页数
         */
        mRecordViewModel?.alwaysPage?.observe(this,Observer{
            binding.TotalPageCount.text = "共${it}页"
        })
        /**
         * 选中
         */
        binding.selectAll.setOnCheckedChangeListener { buttonView, isChecked ->
            mRecordViewModel?.CheckBoxSelect(isChecked)
        }


    }


    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (isShouldHideKeyboard(v, ev)) {
                //隐藏软键盘
                KeyboardUtils.hideSoftInput(binding.NoScrollViewPager)
            }
        }
        return super.dispatchTouchEvent(ev)
    }
}